package com.cl.core.builder;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cl.core.builder.Enum.BuilderType;
import com.cl.core.builder.handler.BuilderHandler;
import com.cl.core.builder.handler.SQLHandler;
import com.cl.core.builder.model.sql.SQLEntity;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.poi.ss.formula.functions.T;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author liddhappy
 * @description 建造工厂
 * @createTime 2022/12/7 22:59
 */
public class BuilderFactory {
    private BuilderFactory() {
    }

    /**
     * 建造过程
     *  @param materials 原料
     * @param results   结果
     */
    public static void builderProcessing(JSONObject materials, List<T> results) throws Exception {
        BuilderHandler builderHandler = null;
        if (ObjectUtils.isNotEmpty(materials)) {
            String builderName = materials.getString("builderName");
            String builderConfig = materials.getString("builderConfig");
            String target = materials.getString("target");
            JSONObject data = materials.getJSONObject("data");
            List<String> builderTypes = Stream.of(BuilderType.values())
                    .map(BuilderType::getBuilderName).collect(Collectors.toList());
            if (builderTypes.contains(builderName)) {
                switch (builderName) {
                    case "SQLBuilder":
                        SQLEntity sqlEntity = JSON.toJavaObject(data.getJSONObject("SQLEntity"), SQLEntity.class);
                        builderHandler = new SQLHandler(builderConfig, target, sqlEntity, results);
                        break;
                    case "JavaObjectBuilder":
                        break;
                    default:
                        break;
                }
            }
            if (ObjectUtils.isNotEmpty(builderHandler)) {
                builderHandler.processBuilder();
            } else {
                throw new Exception("builderHandler not be empty");
            }
        }
    }


}
